<template>
	<view class="content">
		<view>
			地址： <input type="text"v-model="address">
		</view>
		<view>
			数量： <input type="text" v-model='count'>
		</view>
		<view>
			<button @click="send"size="mini" type="warn">发送</button>
		</view>
		<view>
			状态：<text style="color: blue;">{{status}}</text>
		</view>
		<view>
			我的余额：<text style="color: red;">{{balance}}</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				address:'',
				count:'',
				balance:0,
				status:'待处理...',
				MetaCoin:{}
			}
		},
		onLoad() {
  	
	this.init(()=>{
		this.getBlance()
	});
	
  },
		methods:{
	  delBlank(str){
		  return str.replace(/\s*/g,"");
	  },
	  async getBlance(){
		  
		  let balance = await this.MetaCoin.getBalance();
		  
		  
		  this.balance = balance;
	  },
	  statu(str){
		  this.status = str;
	  },
	  send(){
		  let {address,count,delBlank} = this;
		  address = delBlank(address);
		  count = parseInt(count);
		  console.log(address,count);
		  if(address===""||count===""){
			  alert('请输入地址和数量')
		  }
		  else{
			  
			  
			  this.MetaCoin.send(address, count).then(res=>{
			  	console.log(res);
				this.getBlance()
				this.statu('交易成功')
			  	}).catch(err=>{
			  	console.log(err);
			  	})
			 
			 
		  }
		  
	  },
	    async init(callback){
			
			this.$DApp.metaCoin.init().then(res=>{
				this.MetaCoin = res;
				console.log(this.MetaCoin);
				callback();
			})
			
			
			
			
			
			
			
			
		 
	  }
  }
	}
</script>

<style>
	.content{
		padding-top: 100rpx;
		text-align: center;
	}
	view{
		/* display: flex; */
		/* align-items: center; */
	}
	input{
		background-color: #f5f5f5;
		display: inline-block;
		
	}
</style>
